"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.FieldTimeRangePicker = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _jsxRuntime = require("react/jsx-runtime");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _moment = _interopRequireDefault(require("moment"));

var _react = _interopRequireWildcard(require("react"));

/**
 * 时间选择组件
 *
 * @param
 */
var FieldTimePicker = function FieldTimePicker(_ref, ref) {
  var text = _ref.text,
      mode = _ref.mode,
      light = _ref.light,
      label = _ref.label,
      format = _ref.format,
      render = _ref.render,
      renderFormItem = _ref.renderFormItem,
      plain = _ref.plain,
      fieldProps = _ref.fieldProps,
      lightLabel = _ref.lightLabel,
      labelTrigger = _ref.labelTrigger;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      open = _useState2[0],
      setOpen = _useState2[1];

  var size = (0, _react.useContext)(_antdV.ConfigProvider.SizeContext);

  var _useContext = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var prefixCls = getPrefixCls('pro-field-date-picker');
  var finalFormat = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.format) || format || 'HH:mm:ss';
  var isNumberOrMoment = _moment.default.isMoment(text) || typeof text === 'number';

  if (mode === 'read') {
    var dom = (0, _jsxRuntime.jsx)("span", {
      ref: ref,
      children: text ? (0, _moment.default)(text, isNumberOrMoment ? undefined : finalFormat).format(finalFormat) : '-'
    });

    if (render) {
      return render(text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), (0, _jsxRuntime.jsx)("span", {
        children: dom
      }));
    }

    return dom;
  }

  if (mode === 'edit' || mode === 'update') {
    var _dom;

    var disabled = fieldProps.disabled,
        _onChange = fieldProps.onChange,
        placeholder = fieldProps.placeholder,
        allowClear = fieldProps.allowClear,
        value = fieldProps.value;
    var momentValue = (0, _proUtils.parseValueToMoment)(value, finalFormat);

    if (light) {
      var valueStr = momentValue && momentValue.format(finalFormat) || '';
      _dom = (0, _jsxRuntime.jsxs)("div", {
        className: "".concat(prefixCls, "-light"),
        onClick: function onClick(e) {
          var _lightLabel$current, _lightLabel$current$l, _lightLabel$current$l2;

          // 点击label切换下拉菜单
          var isLabelClick = lightLabel === null || lightLabel === void 0 ? void 0 : (_lightLabel$current = lightLabel.current) === null || _lightLabel$current === void 0 ? void 0 : (_lightLabel$current$l = _lightLabel$current.labelRef) === null || _lightLabel$current$l === void 0 ? void 0 : (_lightLabel$current$l2 = _lightLabel$current$l.current) === null || _lightLabel$current$l2 === void 0 ? void 0 : _lightLabel$current$l2.contains(e.target);

          if (isLabelClick) {
            setOpen(!open);
          } else {
            setOpen(true);
          }
        },
        children: [(0, _jsxRuntime.jsx)(_antdV.TimePicker, (0, _objectSpread2.default)((0, _objectSpread2.default)({
          value: momentValue,
          format: format,
          ref: ref
        }, fieldProps), {}, {
          onChange: function onChange(v) {
            _onChange === null || _onChange === void 0 ? void 0 : _onChange(v);
            setTimeout(function () {
              setOpen(false);
            }, 0);
          },
          onOpenChange: function onOpenChange(isOpen) {
            if (!labelTrigger) {
              setOpen(isOpen);
            }
          },
          open: open
        })), (0, _jsxRuntime.jsx)(_proUtils.FieldLabel, {
          label: label,
          disabled: disabled,
          placeholder: placeholder,
          size: size,
          value: valueStr,
          allowClear: allowClear,
          onClear: function onClear() {
            return _onChange === null || _onChange === void 0 ? void 0 : _onChange(null);
          },
          expanded: open,
          ref: lightLabel
        })]
      });
    } else {
      _dom = (0, _jsxRuntime.jsx)(_antdV.DatePicker.TimePicker, (0, _objectSpread2.default)((0, _objectSpread2.default)({
        ref: ref,
        format: format,
        bordered: plain === undefined ? true : !plain
      }, fieldProps), {}, {
        value: momentValue
      }));
    }

    if (renderFormItem) {
      return renderFormItem(text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};
/**
 * 时间区间选择
 *
 * @param param0
 * @param ref
 */


var FieldTimeRangePicker = function FieldTimeRangePicker(_ref2) {
  var text = _ref2.text,
      mode = _ref2.mode,
      format = _ref2.format,
      render = _ref2.render,
      renderFormItem = _ref2.renderFormItem,
      plain = _ref2.plain,
      fieldProps = _ref2.fieldProps;
  var finalFormat = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.format) || format || 'HH:mm:ss';

  var _ref3 = Array.isArray(text) ? text : [],
      _ref4 = (0, _slicedToArray2.default)(_ref3, 2),
      startText = _ref4[0],
      endText = _ref4[1];

  var startTextIsNumberOrMoment = _moment.default.isMoment(startText) || typeof startText === 'number';
  var endTextIsNumberOrMoment = _moment.default.isMoment(endText) || typeof endText === 'number';
  var parsedStartText = startText ? (0, _moment.default)(startText, startTextIsNumberOrMoment ? undefined : finalFormat).format(finalFormat) : '';
  var parsedEndText = endText ? (0, _moment.default)(endText, endTextIsNumberOrMoment ? undefined : finalFormat).format(finalFormat) : '';

  if (mode === 'read') {
    var dom = (0, _jsxRuntime.jsxs)("div", {
      children: [(0, _jsxRuntime.jsx)("div", {
        children: parsedStartText || '-'
      }), (0, _jsxRuntime.jsx)("div", {
        children: parsedEndText || '-'
      })]
    });

    if (render) {
      return render(text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), (0, _jsxRuntime.jsx)("span", {
        children: dom
      }));
    }

    return dom;
  }

  if (mode === 'edit' || mode === 'update') {
    var value = fieldProps.value;
    var momentValue = (0, _proUtils.parseValueToMoment)(value, finalFormat);

    var _dom2 = (0, _jsxRuntime.jsx)(_antdV.TimePicker.RangePicker, (0, _objectSpread2.default)((0, _objectSpread2.default)({
      format: format,
      bordered: plain === undefined ? true : !plain
    }, fieldProps), {}, {
      value: momentValue
    }));

    if (renderFormItem) {
      return renderFormItem(text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), _dom2);
    }

    return _dom2;
  }

  return null;
};

exports.FieldTimeRangePicker = FieldTimeRangePicker;

var _default = /*#__PURE__*/_react.default.forwardRef(FieldTimePicker);

exports.default = _default;